package com.atguigu.flink.watermark;

import com.atguigu.flink.function.WaterSensorMapFunction;
import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.api.common.eventtime.*;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.TimerService;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

/**
 * Created by Smexy on 2022/12/16
 *
 *  自定义的是水印的生成策略
 */
public class Demo6_CustomWaterMark
{
    public static void main(String[] args) {

        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 3333);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);

        //设置水印的发送间隔  2000ms
        env.getConfig().setAutoWatermarkInterval(2000);

        env.setParallelism(1);

        //自定义策略
        //水印具体怎么生成
        WatermarkStrategy<WaterSensor> watermarkStrategy = WatermarkStrategy.forGenerator(context -> new MyWMGeneretor())
            .withTimestampAssigner( (e, r) -> e.getTs());

        env
            .socketTextStream("hadoop103", 8888)
            .map(new WaterSensorMapFunction())
            .assignTimestampsAndWatermarks(watermarkStrategy)
            .process(new ProcessFunction<WaterSensor, WaterSensor>()
            {
                @Override
                public void processElement(WaterSensor value, Context ctx, Collector<WaterSensor> out) throws Exception {

                    //时间服务  1.获取水印  2.注册定时器
                    TimerService timerService = ctx.timerService();

                    // currentWatermark: 数据达到时，算子的当前时钟
                    System.out.println("当前数据达到时，这个算子的水印(时钟)"+timerService.currentWatermark());
                    System.out.println("当前数据达到时，这个算子的物理时钟"+timerService.currentProcessingTime());
                    out.collect(value);
                }
            }).startNewChain()
            .print();


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    //自动延迟1000ms
    private static class MyWMGeneretor implements WatermarkGenerator<WaterSensor>{

        //用来保存当前subTask收到的最大的eventTime
        private long maxTimestamp=Long.MIN_VALUE;
        //间歇性生成
        @Override
        public void onEvent(WaterSensor event, long eventTimestamp, WatermarkOutput output) {
            maxTimestamp = Math.max(maxTimestamp, eventTimestamp);
           /* Watermark watermark = new Watermark(maxTimestamp - 1000);
            output.emitWatermark(watermark);
            System.out.println("向下游发送:"+watermark.getTimestamp());*/


        }

        //周期性生成水印
        @Override
        public void onPeriodicEmit(WatermarkOutput output) {
            Watermark watermark = new Watermark(maxTimestamp - 1000);
            output.emitWatermark(watermark);
            System.out.println("向下游发送:"+watermark.getTimestamp());

        }
    }
}
